using System;
using System.Collections.Generic;

using Csla;

using EntModApp.EntServices.Business;

using DemoApp.DataAccess;

namespace DemoApp.BusinessLayer 
{
  public partial class CslaGroupInfo : 
    CslaUserInfo, 
    IGroup
  {
    #region [ Business Properties ]

    public static PropertyInfo<CslaUserInfoCollection> UsersPropertyInfo = RegisterProperty(new PropertyInfo<CslaUserInfoCollection>("Users"));
    public CslaUserInfoCollection Users 
    { 
      get
      {
        return ReadProperty(UsersPropertyInfo);
      }
      private set
      {
        LoadProperty(UsersPropertyInfo, value);
      }
    }

    #endregion [ Business Properties ]

    #region [ Factory Methods ]
    protected internal static CslaGroupInfo GetGroupInfo(Group data, GroupLoadSettings loadSettings)
    {
      if (data == null) throw new ArgumentNullException("data");
      if (loadSettings == null) throw new ArgumentNullException("loadSettings");
      CslaGroupInfo result = new CslaGroupInfo();
      result.FetchGroup(data, loadSettings);
      return result;
    }

    private static int ForceInitField = 0;
    protected CslaGroupInfo() 
    {
      ForceInitField = 1;
    }
    #endregion [ Factory Methods ]

    #region [ Data Access ]
    protected void FetchGroup(Group data, GroupLoadSettings loadSettings)
    {
      FetchGroupData(data, loadSettings);
      FetchGroupChildren(data, loadSettings);
    }

    protected void FetchGroupData(Group data, GroupLoadSettings loadSettings)
    {
      base.FetchUserData(data, loadSettings);
    }

    protected void FetchGroupChildren(Group data, GroupLoadSettings loadSettings)
    {
      base.FetchUserData(data, loadSettings);
      if (loadSettings.Inclusion.IncludeUsers)
        this.Users = CslaUserInfoCollection.GetUserInfoCollection(data.Users, loadSettings.Inclusion.IncludedUsersSettings);
    }
    #endregion [ Data Access ]

    #region [ Explicit IGroup implementation ]

    IBusinessObjectCollection<IUser> IGroup.Users
    { 
      get
      {
        return this.Users;
      }
    }


    bool IBusinessObject.IsReadonly
    {
      get { return true; }
    }

    bool IBusinessObject.IsDirty
    {
      get { return false; }
    }

    #endregion [ Explicit IGroup implementation ]
  }
}
